package edu.sctu.jarime.june.day20150615.copyFile;
import java.io.*;

/**
 * Created by user19 on 2015/6/15.
 */
public class CopyFileByDifferentWay {
    /**
     * 一个字节一个字节复制
     * @param srcFile
     * @param destFile
     * @throws Exception
     */
    public static void copyFileByByte(File srcFile,File destFile)throws Exception {
        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件" + srcFile + "不存在");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件");
        }
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);

        int a;
        while ((a = fis.read()) != -1) {//读出来赋值给a
            fos.write(a);//写入新的文件中
            fos.flush();//刷新
        }
        fis.close();
        fos.close();
    }

    /**
     * 使用byte数组复制
     * @param srcFile
     * @param destFile
     * @throws Exception
     */
    public static void copyFileByByteArray(File srcFile,File destFile)throws Exception{
        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件" + srcFile + "不存在");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件");
        }
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);


        byte[] bytes = new byte[2014];

        while (( fis.read(bytes)) != -1) {//读出来赋值给a
            fos.write(bytes, 0, bytes.length);//写入新的文件中
            fos.flush();//刷新
        }
        fis.close();
        fos.close();
    }

    /**
     * 使用Datainputstream和DataoutputStream写复制文件
     * @param srcFile
     * @param destFile
     * @throws Exception
     */
    public static void copyFileByData(File srcFile,File destFile)throws Exception{
        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件" + srcFile + "不存在");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件");
        }
        DataInputStream dis = new DataInputStream(new FileInputStream(srcFile));
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(destFile));

        int c;
        while ((c=dis.read())!=-1){
            dos.writeByte(c);
            dos.flush();
        }
        dis.close();
        dos.close();
    }


    /**
     * 使用缓冲带复制
     * @param srcFile
     * @param destFile
     * @throws Exception
     */
    public static void copyFileByBuffer(File srcFile,File destFile)throws Exception{
        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件" + srcFile + "不存在");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件");
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
        int d;
        while ((d=bis.read())!=-1){
            bos.write(d);
            bos.flush();
        }
        bos.close();
    }
}
